/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.workingtimemanager.urb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.workingtimemanager.urb.db.DatabaseHandler;
import de.workingtimemanager.urb.entities.Project;
import de.workingtimemanager.urb.entities.User;

/**
 *
 * @author CK05102
 */
public class ViewProjectsActivity extends Activity {

    Context context;
    DatabaseHandler db;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here    
        setContentView(R.layout.viewprojects);

        db = new DatabaseHandler();
        context = this;

        createView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setContentView(R.layout.viewprojects);
        createView();
    }

    private void createView() {
        LinearLayout layout = (LinearLayout) findViewById(R.id.viewprojects_projectbuttons_layout);
        layout.removeAllViewsInLayout();

        try {
            db.open(this);
            //reset old active
            Project oldActive = db.getActiveUser().getActiveProject();
            if (oldActive != null) {
                oldActive.setActive(false);
            }
            db.insert(oldActive);
            User user = db.getActiveUser();


            TextView userView = (TextView) findViewById(R.id.viewprojects_activeuser_text);
            userView.setText(user.getUSERNAME());

            for (int i = 1; i < user.getProjects().size(); i++) {
                final Button button = new Button(this);
                button.setText(user.getProjects().elementAt(i).getProjectName());
                layout.addView(button);

                button.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        try {
                            db.open(context);
                            User activeUser = db.getActiveUser();
                            Project activeProject = activeUser.getProjectByName(button.getText().toString());
                            activeProject.setActive(true);
                            db.insert(activeProject);
                        } finally {
                            db.close();
                        }
                        startActivity(new Intent(context, ViewTimersActivity.class));
                    }
                });
            }

            final Button createButton = (Button) findViewById(R.id.viewprojects_createproject_button);
            createButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    startActivity(new Intent(context, CreateProjectActivity.class));
                }
            });
        } finally {
            db.close();
        }
    }
}
